package com.gitee.zhangchenyan.takin.common.inject;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Data
@Configuration
@EnableSwagger2WebMvc
public class GlobalSwaggerConfig {

    @Autowired
    private Environment environment;

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {

        String title = environment.containsProperty("swagger.title") ? environment.getProperty("swagger.title") : "Takin RESTful API";
        String description = environment.containsProperty("swagger.description") ? environment.getProperty("swagger.description") : "基于Takin框架的API";
        String url = environment.containsProperty("swagger.url") ? environment.getProperty("swagger.url") : "https://gitee.com/zhangchenyan/takin.git";
        String version = environment.containsProperty("swagger.version") ? environment.getProperty("swagger.version") : "1.1.1";
        String groupName = environment.containsProperty("swagger.groupName") ? environment.getProperty("swagger.groupName") : "takin";

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(title)
                        .description(description)
                        .termsOfServiceUrl(url)
                        .version(version)
                        .build())
                //分组名称
                .groupName(groupName)
                .select()
                .paths(PathSelectors.any())
                .build();
    }
}

